/*
 * @Description: 文件参数处理
 * @Autor: HuiSir<www.zuifengyun.com>
 * @Date: 2022-05-27 16:32:01
 * @LastEditTime: 2022-06-07 15:29:53
 */
import Router from '@koa/router'
export default () => {
    const midware: Router.Middleware = async (ctx, next) => {
        const { header, files } = ctx.request
        const isFormData = header['content-type']?.includes('multipart/form-data')

        // 文件
        if (isFormData && Object.keys(files).length) {
            for (const key in files) {
                const file = files[key]
                if (Array.isArray(file)) {
                    ctx.request.body[key] = file.map(({ size, path, name, type, uploadPath }: any) => ({ size, path, name, type, uploadPath }))
                } else {
                    const { size, path, name, type, uploadPath }: any = file
                    ctx.request.body[key] = { size, path, name, type, uploadPath }
                }
            }
        }

        await next()
    }

    return midware
}
